package com.stu.servlet;

import com.stu.dao.ManageDao;
import com.stu.dao.impl.ManageDaoImpl;
import com.stu.model.User;

import com.stu.util.DaoUtile;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@WebServlet("/ManagerServlet")
public class ManagerServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //获取用户的动作
        String action = req.getParameter("action");
        //创建管理对象
        ManageDaoImpl md = new ManageDaoImpl();
        //点击人员管理的按钮或查找按钮或删除按钮
        if (action.equals("allUserList") || action.equals("search") || action.equals("delThisPerson")) {
            List<User> userList = null;
            if (action.equals("allUserList") || action.equals("delThisPerson")) {
                if (action.equals("delThisPerson")) {
                    String delAccount = req.getParameter("userAccount");
                    String userIdentify = req.getParameter("userIdentify");
                 /*   System.out.println(delAccount);
                    System.out.println(userIdentify);*/
                    try {
                        //根据删除的账户和使用者的账户删除人员
                        md.delThisPerson(delAccount, userIdentify);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
                try {
                    userList = md.getAllUser();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            } else {
                String searchText = req.getParameter("searchText").trim();
                try {
                    userList = md.getSearch(searchText);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            //将用户集合作为参数传递到主页面
            req.setAttribute("userList", userList);
            //将user.jsp页面作为参数传递到主页面
            req.setAttribute("mainRight", "user.jsp");
            //跳转主页面
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
            //点击添加人员的按钮
        } else if (action.equals("goAddPerson")) {
            //加载addPerson.jsp页面
            req.setAttribute("mainRight", "addPerson.jsp");
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);

        } else if (action.equals("addThisPerson")) {
            String newUserAccount = null;
            try {
                newUserAccount = DaoUtile.getPersonAccount();
                System.out.println(newUserAccount);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            String userName = req.getParameter("userName");//姓名
       /*     System.out.println(userName);*/
            String userSex = req.getParameter("userSex");//性别
      /*      System.out.println(userSex);*/
            String userBirthday = req.getParameter("userBirthday");//出生
        /*    System.out.println(userBirthday);*/
            String userIdCard = req.getParameter("userIdCard");
           /* System.out.println(userIdCard);*/
            String userPassword = req.getParameter("userPassword");
           /* System.out.println(userPassword);*/
            int userIdentify = Integer.parseInt(req.getParameter("userIdentify"));
           /* System.out.println(userIdentify);*/
            String userOtherName = req.getParameter("userOtherName");
            User newUser = new User(newUserAccount, userName, userSex, userBirthday, userIdCard, userPassword, userIdentify, userOtherName);
            try {
                md.addUser(newUser);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            req.setAttribute("tip", "添加完成");
            //将user.jsp页面作为参数传递到主页面
            req.setAttribute("mainRight", "blank.jsp");
            //跳转主页面
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
        }
    }
}
